package com.ij.media.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ij.media.enums.RequestTypeEnums;
import com.ij.media.service.SpiderService;
import com.ij.media.utils.RequestType;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class SpiderServiceImpl implements SpiderService {
    @Override
    public JSONObject testRequest(String url, RequestTypeEnums enums) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        headers.set("Cookie","bid=8A4Sqj6ORZs; douban-fav-remind=1; viewed=\"4898293\"; gr_user_id=4f766ba4-d625-4377-a205-e139b6746971; _vwo_uuid_v2=D8600C0D7DD39BC210E9AC9835E484666|da3a8900f2a91bc2fdff5abe962a3c3d; ll=\"118163\"; __yadk_uid=gU8pb1FQ4zQiIIUR2h63BNN7W1eqA7tc; trc_cookie_storage=taboola%2520global%253Auser-id%3D00732ef8-2a13-4c23-ab47-87aa0d347b1a-tuct2e2d593; __gads=ID=b29a75062c204d70:T=1558056512:S=ALNI_MbFbPnIVmx56QKSLWgauE5L8m-I3g; __utmz=30149280.1559039583.3.3.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided); __utmz=223695111.1559039583.3.3.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided); ct=y; _pk_ref.100001.4cf6=%5B%22%22%2C%22%22%2C1559802314%2C%22https%3A%2F%2Fwww.google.com%2F%22%5D; ap_v=0,6.0; __utma=30149280.376666775.1557882914.1559096063.1559802314.5; __utmc=30149280; __utma=223695111.1236026819.1557882914.1559096063.1559802314.5; __utmc=223695111; _pk_id.100001.4cf6=ab11e44519b962d3.1557882914.5.1559802339.1559096072.");
        headers.set("Host","movie.douban.com");
        headers.set("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36");
        HttpEntity<String> entity  = new HttpEntity<String>(headers);
        String strBody = restTemplate.exchange(url, RequestType.returnType(enums),entity,String.class).getBody();
        return JSONObject.parseObject(strBody);
    }

    @Override
    public JSONObject testGet(String url) {
        return null;
    }
}
